/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * InventoryList Class:
 * This class is separate from the Inventory class, but enables the Inventory and List of Inventory available to be modified
 * @author Vale
 *
 */
public class InventoryList {

    private ArrayList<Inventory> inventList = new ArrayList<Inventory>();
    private final String PRINT_CHOICE_INFO = "Edit the number of Inventory Items available by entering the corresponding number in the list above, or return to the main menu (Option: 0)";
    private final String INVALID_OPTION = "You did not enter a valid option.";
    private final String ERR = "Error: ";
    private final String REENTER_CHOICE = "Please enter your choice again.";
    private final String OPT = "Option: ";
    private final int ZERO = 0;
    private final int FALSE = -1;
    private final String CURR_INV = "The Inventory currently on hand is:";
    private final String AVAIL = " Available";
    private final String EDIT_INV = "You are currently editing the Inventory Item: ";
    private final String INPUT_INV = "Please enter the current inventory for this Product: ";

    /**
     * void add(Inventory inv) Method:
     * Adds an Inventory Item (inv) to the Inventory List
     *
     */
    public void add(Inventory inv) {
        inventList.add(inv);
    }

    /**
     * void remove(int index) Method:
     * Removes Inventory Item at index, from the Inventory List
     *
     */
    public void remove(int index) {
        inventList.remove(index);
    }

    /**
     * void remove(Inventory inv) Method:
     * Removes given Inventory Item (inv) from the Inventory List
     *
     */
    public void remove(Inventory inv) {
        inventList.remove(inv);
    }

    /**
     * boolean isEmpty() Method:
     * Checks if Inventory List is empty
     * @return True if empty, false otherwise
     *
     */
    public boolean isEmpty() {
        return inventList.isEmpty();
    }

    /**
     * int indexOf(Inventory inv) Method:
     * Returns index of given Inventory ITem
     * @param inv The Inventory Item whose index is sought
     * @return Returns index of given Inventory ITem
     *
     */
    public int indexOf(Inventory inv) {
        return inventList.indexOf(inv);
    }

    /**
     * int size() Method:
     * Returns Size of Inventory List
     * @return Returns Size of Inventory List
     *
     */
    public int size() {
        return inventList.size();
    }

    /**
     * Product get(int index) Method:
     * Returns Inventory Item from Inventory List at given index
     * @param index The index of the Inventory Item
     * @return Returns Inventory Item from Inventory List at given index
     *
     */
    public Inventory get(int index) {
        return inventList.get(index);
    }

    /**
     * ArrayList<Inventory> getInvList() Method:
     * Returns List of Inventory Items
     * @return Returns List of Inventory Items
     *
     */
    public ArrayList<Inventory> getInvList() {
        return inventList;
    }

    /**
     * void printInventory() Method:
     * Prints all Inventory Items from Inventory List to Screen. Current Inventory can then be edited
     * @return Returns when User is finished viewing Inventory
     *
     */
    public void printInventory() {
        String choice = null;
        System.out.println(CURR_INV);
        for (int i = 0; i < inventList.size(); i++) {
            System.out.print('\t' + String.valueOf(i + 1) + ". ");
            System.out.print(inventList.get(i).getInvName() + " - ");
            System.out.println(inventList.get(i).getInvStock() + AVAIL);
        }
        System.out.println(PRINT_CHOICE_INFO);
        System.out.print(OPT);
        try {
            BufferedReader choiceIn = new BufferedReader(new InputStreamReader(System.in));
            choice = choiceIn.readLine();
            int checkChoice = checkInput(choice);
            if ((checkChoice >= ZERO) && (checkChoice <= inventList.size())) {
                if (checkChoice == ZERO) {
                    return;
                } else if (checkChoice == FALSE) {
                    System.out.println(INVALID_OPTION);
                    printInventory();
                } else {
                    editInventItem(checkChoice - 1);
                    printInventory();
                }
            } else {
                System.out.println(INVALID_OPTION);
                printInventory();
            }
        } catch (IOException e) {
            System.out.println(ERR + e);
            System.out.println(REENTER_CHOICE);
            printInventory();
        }
    }

    /**
     * void editInventItem(int inv) Method:
     * Edits the Number of the specified inventory items in stock
     * @param inv Number in stock
     * @return Function returns to Main Menu/Product Menu pending on user input
     *
     */
    public void editInventItem(int inv) {
        String inventAvail = null;
        System.out.println(EDIT_INV + inventList.get(inv).getInvName());
        System.out.print(INPUT_INV);
        try {
            BufferedReader invIn = new BufferedReader(new InputStreamReader(System.in));
            inventAvail = invIn.readLine();
            int checkChoice = checkInput(inventAvail);
            if (checkChoice == FALSE) {
                System.out.println(INVALID_OPTION);
                editInventItem(inv);
            } else {
                inventList.get(inv).setInvStock(checkChoice);
                return;
            }
//            } else {
//                System.out.println("You did not enter a valid option.");
//                printInventory();
        } catch (IOException e) {
            System.out.println(ERR + e);
            System.out.println(REENTER_CHOICE);
            printInventory();
        }
    }

    /**
     * int checkInput(String choice) Method:
     * Takes the input choice from the main menu and checks to make sure it is correct.
     * @param choice This is the input String as entered by the user
     * @return Null String, invalid numbers or numbers out of bounds of array return FALSE, choiceIn otherwise
     *
     */
    public int checkInput(String choice) {
        try {
            int choiceIn = Integer.parseInt(choice);
            if (choice.equals("")) {
                return FALSE;
            } else {
                return choiceIn;
            }
        } catch (NumberFormatException x) {
            return FALSE;
        }
    }
}
